package com.qingxin.wuye.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.entity.WyRoom;


@Service
public class WyRoomService extends BaseService<WyRoom, String> {

	
	public WyRoom getWyRoomByRoomCode(String buildingid, String roomCode){
		WyRoom room = (WyRoom) getByHql("from WyRoom wyBuilding.buildingid=? and roomCode=?", buildingid, roomCode);
		return room;
	}
	
	
	/**
	 * 获取列表数据
	 * @param companyid
	 * @param gardenid
	 * @param buildingid
	 * @param roomCode
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String companyid, String gardenid, String buildingid, String roomCode, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.wuye.entity.WyRoom(" +
				"r.roomid, r.roomCode, r.buildingArea, r.roomArea, r.publicArea, r.tollTarget, r.floorNum," +
				" r.address, r.roomStatus, r.roomType, r.checkinDate, r.leaseStatus, r.ownership, r.roomUse," +
				" r.decorative, r.unitNo, r.orientation, r.pact, r.rent, r.mgrfee, r.price, r.totalPrice," +
				" r.leaseContractNo, r.remark, g.gardenName, b.buildingName)" +
				" from WyRoom r, WyBuilding b, WyGarden g" +
				" where r.wyBuilding.buildingid=b.buildingid and b.wyGarden.gardenid=g.gardenid ");
		
		if(StringUtils.isNotEmpty(companyid) && !"0".equals(companyid)){
			querier.append(" and g.wyPropertyCompany.companyid=:companyid ");
			querier.setParam("companyid", companyid);
		}
		
		if(StringUtils.isNotEmpty(gardenid) && !"0".equals(gardenid)){
			querier.append(" and b.wyGarden.gardenid=:gardenid ");
			querier.setParam("gardenid", gardenid);
		}
		
		if(StringUtils.isNotEmpty(buildingid) && !"0".equals(buildingid)){
			querier.append(" and r.wyBuilding.buildingid=:buildingid ");
			querier.setParam("buildingid", buildingid);
		}else {
			if(!SystemCfg.isDataAdminUser(loginUser)){
				querier.append(" and b.buildingid in (select distinct v.wyBuilding.buildingid from SysUserBuildingRel v where v.sysUser.userid=:userid)");
				querier.setParam("userid", loginUser.getUserid());
			}
		}
		
		if(StringUtils.isNotEmpty(roomCode)){
			roomCode = roomCode.replace("'", "");
			querier.append(" and r.roomCode like :roomCode ");
			querier.setParam("roomCode", "%"+roomCode+"%");
		}
		
		querier.append(pageParam.getOrderby(" order by b.createTime desc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 添加信息
	 * @param room
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(WyRoom room, LoginUser loginUser, HttpServletRequest request){
		
		Date now = new Date();
		room.setCreateTime(now);
		room.setUpdateTime(now);
		room.setCreateBy(loginUser.getLogin_name());
		room.setUpdateBy(loginUser.getLogin_name());
		
		this.save(room);
		
		return null;
		
	}
	
	
	/**
	 * 修改信息
	 * @param room
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(WyRoom room, LoginUser loginUser, HttpServletRequest request){
		WyRoom old = getSafeEntity(room.getRoomid());
		if(old == null){
			return new SysError("参数错误");
		}
		
		String fields = "roomCode,buildingArea,roomArea,publicArea,tollTarget,floorNum,address,roomStatus,roomType," +
				"checkinDate,leaseStatus,ownership,roomUse,decorative,unitNo,orientation,pact,rent,mgrfee,price,totalPrice,leaseContractNo,remark";
		BeanUtil.setTargetFields(old, room, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<WyRoom> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		
		return null;
		
	}
	
	
	/**
	 * 删除信息
	 * @param roomids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] roomids, LoginUser loginUser, HttpServletRequest request){
		for(String roomid : roomids){
			WyRoom room = get(roomid);
			if(room==null){
				continue;
			}
			
			this.delete(room);
		}
		return null;
	}
	
	
	
	
}
